//dfs搜索出答案
#include <iostream>
#include <cstring>

using namespace std;

const int N = 15;

typedef long long LL;

int n;

bool st[N], flag;

LL later, res;

void dfs(int u, int sum)
{
	if (flag) return;
	if (u == n)
	{
		if (flag) return;
		if (sum == later) 
		{
			flag = true;
			return ;	
		}
		else res = sum;
		
		return ;
	}
	
	for (int i = 1; i <= n; i ++ )
	{
		if (st[i]) continue;
		st[i] = true;
		dfs(u + 1, sum * 10 + i);
		st[i] = false;
	}
}

int main()
{
	cin >> n;
	
	for (int i = 0; i < n; i ++ )
	{
		int v;
		cin >> v;
		
		later = later * 10 + v;
	}
	
	dfs(0, 0);
	
	if (!res) puts("ERROR");
	else
	{
		auto s = to_string(res);
		
		for (int i = 0; i < s.size(); i ++ ) cout << s[i] << ' ';
		
	}
	
	return 0;
}
//-------------------------------------
//stl大法
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 20;

int a[N];

int n;

int main()
{
	cin >> n;
	
	for (int i = 0; i < n; i ++ ) cin >> a[i];
	
	if (prev_permutation(a, a + n))
	{
		for (int i = 0; i < n; i ++ ) cout << a[i] << ' ';
	}
	else cout << "ERROR" << endl;
	
	return 0;
}
//康托展开与逆展开
#include <iostream>
#include <vector>

using namespace std;

const int N = 20;

int a[N], fact[N];
vector<int> v;

int n;

void init()
{
	fact[1] = 1;
	
	for (int i = 2; i <= n - 1; i ++ ) fact[i] = fact[i - 1] * i;
}

int cantor()
{
	int res = 0;
	
	for (int i = 0; i < n; i ++ )
	{
		int k = 0;
		
		for (int j = i + 1; j < n; j ++ )
			if (a[j] < a[i]) k ++ ;
			res += k * fact[n - i - 1];
	}
	
	return res;
}

int incantor(int t)
{
	int res = 0;
	
	
	
	for(int i=1; i<=n; ++i) v.push_back(i);
	
	for (int i = 0; i < n - 1; i ++ )
	{
		int k = t / fact[n - i - 1];
		int m = t % fact[n - i - 1];
		
		res = res * 10 + v[k];
		v.erase(v.begin() + k);
		
		t = m;
	}
	
	res = res * 10 + v[0];
	
	return res;
}

int main()
{
	cin >> n;
	
	init();
	
	for (int i = 0; i < n; i ++ ) cin >> a[i];
	
	int t = cantor();
	
//	cout << t << endl;
	
	int res = incantor( -- t);
	
//	cout << res << endl;
	auto s = to_string(res);
	
	for (int i = 0; i < n; i ++ ) cout << s[i] << ' ';
	
	return 0;
}
